﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Solidi.Tests
{
	public class MockDependencyRegistry : IDependencyRegistry
	{
		private IDependencyRegistry _registry;

		public MockDependencyRegistry(IDependencyRegistry registry)
		{
			_registry = registry;
		}

		public void Add(IEnumerable<IDependency> dependencies)
		{
			_registry.Add(dependencies);
		}

		public IEnumerable<IDependency> Find(IDependencyKey key, Func<IEnumerable<IDependency>> dependencyFactory = null)
		{
			return _registry.Find(key).Select(d => Mock(d)).ToArray();
		}

		private IDependency Mock(IDependency dependency)
		{
			Type type = dependency.Key.Type;

			if (IsCandidiateType(type))
			{
				return new Mock(dependency, type);
			}

			return dependency;
		}

		private static bool IsCandidiateType(Type type)
		{
			return type.IsClass && !type.IsSubclassOf(typeof(Delegate));
		}
	}
}
